///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.framework {
	import com.asual.swfaddress.*;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	import flash.system.Capabilities;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	
	import sugarcookie.display.ApplicationBase;
	import sugarcookie.error.ApplicationError;
	import sugarcookie.events.DataEvent;
	import sugarcookie.events.GenericEvent;
	import sugarcookie.framework.IModel;
	import sugarcookie.style.StyleManager;
	
	/**
	 * Starting point for the framework, handles basic set-up routines.
	 */
	public class Application extends ApplicationBase {
		public static const TRACKING:String = "onTracking";
		
		protected var _data:IModel;
		protected var _displayManager:ViewStack;
		protected var _styleManger:StyleManager;
		protected var _lang:String;
		protected var _configUrl:String;
		protected var _initDeepLink:String;
		protected var _hostPage:String;
		protected var _useSWFAddress:Boolean;
		
		/**
		 * @param useAppLoader Lets the application know if a preloader has been defined.  If one is not and this is not set to false, the application will stall out.  When set to false the private startup method is automatically called.
		 */
		public function Application(useLogging:Boolean=true,useSWFAddress:Boolean=true,useAppLoader:Boolean=true,tabChildren:Boolean=false) {
			super(useLogging,useAppLoader,tabChildren);
			
			_log = Log.getLogger("sugarcookie.framework.Application");
			_useSWFAddress = useSWFAddress;
		}
		
		/**
		 * Starts up the application.  This is normally called by the AppLoader class after the base swf has preloaded, but can be called automatically by passing false into the constructor.
		 */
		override public function startup():void {
			_log.debug("startup()");
			
			if (_useSWFAddress) {
				SWFAddress.onInit = getDeepLink;
			} else {
				initData();
				addListener();
			}
		}
		
		/**
		 * Sets the initial value for _initDeepLink and then moves the application along by calling initData() and then automatically calling addListener() to add a listener to the DataManager for the DataManager.ON_DATA_INIT event which signals that all initial data has been loaded and processd.
		 * @private
		 */
		private function getDeepLink():void {
			_initDeepLink = SWFAddress.getValue();
			_log.debug("getDeepLink(): "+_initDeepLink);
			initData();
			addListener();
		}
		
		override protected function customDefaults():void {
			_lang = loaderInfo.parameters.lang!=undefined?String(loaderInfo.parameters.lang).toUpperCase():"EN"
            _hostPage = loaderInfo.parameters.page!=undefined?loaderInfo.parameters.page:"home";
            _configUrl = loaderInfo.parameters.config!=undefined?loaderInfo.parameters.config:"./xml/config.xml";
		}
		
		/**
		 * This method is called during the start up process of the application.  It is intended to be overridden so that a custom subclass of DataManager can be used.  If it is not the default DataManager will be instantiated.
		 */
		protected function initData():void {
			_log.info("Application.initData(): can be overridden to instantiate a subclass of DataManager to provide custom functionality.");
			//_data = new DataManager(_configUrl,_initDeepLink,_hostPage);
		}
		
		/**
		 * Automatically adds the listener to the DataManager.ON_DATA_INIT event.
		 * @private
		 */
		private function addListener():void {
			_data.addEventListener(DataEvent.ON_DATA_INIT,onDataInit,false,0,true);
		}
		
		/**
		 * Handles the DataManager.ON_DATA_INIT event. Facilitats the removal of the event listener and calls the next function in the application instantiation sequence.
		 * @private
		 */
		private function onDataInit(evt:GenericEvent):void {
			_data.removeEventListener(DataEvent.ON_DATA_INIT,onDataInit);
			initApp();
		}
		
		/**
		 * Automatically called after all initial data has been received and processed.  This must be overridden by the subclass.  If you are not using a preloader, the ViewStack subclass may be instantiated here.  Otherwise it is more important to complete any additional top level setup and then dispatch the AppLoader.APPLICATION_READY event.
		 */
		protected function initApp():void {
			//overridden by the subclass
			throw new ApplicationError("Application.initApp() method must be overridden by subclass.");
		}
	}
}